function check_d_value()
{
        var accountId = document.getElementById('d_accountId0').value;
        var DRamount = document.getElementById('d_DRamount0').value;
        var CRamount = document.getElementById('d_CRamount0').value;
        var description = document.getElementById('d_description0').value;
        
        var bool = true;
        
        if(accountId=="")
        { 
                bool = false;
        }
        if(DRamount==""&&CRamount=="")
        { 
                bool = false;
        }
        if(DRamount!=""&&CRamount!="")
        {
                document.getElementById('d_DRamount0').value = "";
                document.getElementById('d_CRamount0').value = "";
                bool = false;
        }
        if(has_input_DR&&CRamount!="")
        {
                document.getElementById('d_CRamount0').value = "";
                bool = false;
        }
        if(has_input_CR&&DRamount!="")
        {
                document.getElementById('d_DRamount0').value = "";
                bool = false;
        }
        if(description=="")
        {
                bool = false;
        }
        return bool;
}
function check_g_value()
{
        var accountId = document.getElementById('g_accountId0').value;
        var DRamount = document.getElementById('g_DRamount0').value;
        var CRamount = document.getElementById('g_CRamount0').value;
        var description = document.getElementById('g_description0').value;
        
        var bool = true;
        
        if(accountId=="")
        { 
                bool = false;
        }
        if(DRamount==""&&CRamount=="")
        { 
                bool = false;
        }
        if(DRamount!=""&&CRamount!="")
        {
                document.getElementById('g_DRamount0').value = "";
                document.getElementById('g_CRamount0').value = "";
                bool = false;
        }
        if(description=="")
        {
                bool = false;
        }
        return bool;
}
function add_d_row()
{
        if(check_d_value())
        {
        
                var dl = document.createElement('dl');
                dl.setAttribute('class','fin-title fin-form2');
                dl.setAttribute('className','fin-title fin-form2');
                dl.setAttribute('id','ditem'+i); 


                var dd = document.createElement('dd');
                var accountId = document.getElementById('d_accountId0').value;
                dd.innerHTML = '<input type="hidden" name="d_accountId" value="'+accountId+'"/><a href="/bin/finance/fa-sl.py?sl={{info_sl}}&a='+accountId+'">'+accountId+'</a>';             
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var DRamount0 = document.getElementById('d_DRamount0').value;
                var DRamount = 0; 
                if(DRamount0!=''){
                        has_input_DR = true;
                        DRamount += parseFloat(DRamount0);
                } 
                if(DRamount == 0){
                        dd.innerHTML = '<input type="hidden" name="d_DRamount" value=""/>&nbsp;';
                }else{
                        dd.innerHTML = '<input type="hidden" name="d_DRamount" value="'+DRamount+'"/>'+DRamount;
                } 
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var CRamount0 = document.getElementById('d_CRamount0').value;
                var CRamount = 0;                
                if(CRamount0!=''){
                        has_input_CR = true;
                        CRamount += parseFloat(CRamount0);
                }
                if(CRamount == 0){
                        dd.innerHTML = '<input type="hidden" name="d_CRamount" value=""/>&nbsp;';
                }else{
                        dd.innerHTML = '<input type="hidden" name="d_CRamount" value="'+CRamount+'"/>'+CRamount;
                }
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var description = document.getElementById('d_description0').value;
                dd.innerHTML='<input type="hidden" name="d_description" value="'+description+'" />'+description;               
                dl.appendChild(dd); 
                
                var dt = document.createElement('dt');
                dt.innerHTML='<a href="javascript:void(0)" onclick="del_row(\'ditem'+i+'\');check_balance(gSumOldDrValue,gSumOldCrValue,'+DRamount+','+CRamount+',0,1);">Delete</a>';
                dl.appendChild(dt);                

                obj1.appendChild(dl); 
                dSumDrValue += parseFloat(DRamount);
                dSumCrValue += parseFloat(CRamount); 
                sum_d_to_g(dSumDrValue,dSumCrValue);
                
                i += 1;
                clean_up('d');                
                check_balance(gSumOldDrValue,gSumOldCrValue,DRamount,CRamount,1,0);                
        }
}
function add_g_row()
{        
        if(check_g_value())
        {
        
                var dl = document.createElement('dl');
                dl.setAttribute('class','fin-title fin-form2');
                dl.setAttribute('className','fin-title fin-form2');
                dl.setAttribute('id','gitem'+j); 
                
                var dd = document.createElement('dd');                
                var accountId = document.getElementById('g_accountId0').value;
                dd.innerHTML = '<input type="hidden" name="g_accountId" value="'+accountId+'"/><a href="/bin/finance.py/show_account_details?a='+accountId+'">'+accountId+'</a>';
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var DRamount0 = document.getElementById('g_DRamount0').value;
                var DRamount = 0;                
                if(DRamount0!=''){                      
                        DRamount += parseFloat(DRamount0);
                }
                if(DRamount == 0){
                        dd.innerHTML = '<input type="hidden" name="g_DRamount" value=""/>&nbsp;';
                }else{
                        dd.innerHTML = '<input type="hidden" name="g_DRamount" value="'+DRamount+'"/>'+DRamount;
                }
                dl.appendChild(dd); 
                
                var dd = document.createElement('dd');
                var CRamount0 = document.getElementById('g_CRamount0').value;
                var CRamount = 0;                
                if(CRamount0!=''){                      
                        CRamount += parseFloat(CRamount0);
                }
                if(CRamount == 0){
                        dd.innerHTML = '<input type="hidden" name="g_CRamount" value=""/>&nbsp;';
                }else{
                        dd.innerHTML = '<input type="hidden" name="g_CRamount" value="'+CRamount+'"/>'+CRamount;
                }
                dl.appendChild(dd);
                
                var dd = document.createElement('dd');
                var description = document.getElementById('g_description0').value;
                dd.innerHTML='<input type="hidden" name="g_description" value="'+description+'" />'+description;               
                dl.appendChild(dd);
                
                var dt = document.createElement('dt');
                dt.innerHTML = '<a href="javascript:void(0)" onclick="del_row(\'gitem'+j+'\');check_balance(gSumOldDrValue,gSumOldCrValue,'+DRamount+','+CRamount+',0,0);">Delete</a>';
                dl.appendChild(dt);

                obj2.appendChild(dl);            
                j += 1;
                clean_up('g');
                check_balance(gSumOldDrValue,gSumOldCrValue,DRamount,CRamount,1,0);
        }
}
function sum_d_to_g(num1,num2)
{
        document.getElementById('td_g_CR').innerHTML = '<input type="hidden" name="old_g_CRamount" value="'+num2+'"/>'+num2;
        document.getElementById('td_g_DR').innerHTML = '<input type="hidden" name="old_g_DRamount" value="'+num1+'"/>'+num1;
}
function check_balance(num1,num2,num3,num4,flag,flag2)
{      
        var old_total_DR = parseFloat(num1);
        var old_total_CR = parseFloat(num2);   
        
        var del_DR = 0;
        var del_CR = 0; 
        if(num3==""){
                del_DR += 0;
        }else{
                del_DR += parseFloat(num3);
        }
       
        if(num4==""){
                del_CR += 0;
        }else{
                del_CR += parseFloat(num4);
        }        
        
        if(flag==0){
                gSumOldDrValue = old_total_DR - del_DR;
                gSumOldCrValue = old_total_CR  - del_CR;
        }else{
                gSumOldDrValue = old_total_DR + del_DR;
                gSumOldCrValue = old_total_CR + del_CR;
        }        
        if(flag2==1){
                dSumDrValue -= del_DR;
                dSumCrValue -= del_CR;
                sum_d_to_g(dSumDrValue,dSumCrValue);
        }
        if(flag2==2){
                if(gSumOldDrValue == gSumOldCrValue){
                        return true;
                }else{
                        return false;
                }
        }
}
function del_row(id){
    del_Tr = document.getElementById(id);
    len = del_Tr.childNodes.length;
    for(j=0;j<len;j++){
            del_Tr.childNodes[j].innerHTML='';
    }
   del_Tr.style.display='none' ;
}

function clean_up(str)  
{   
        document.getElementById(str+'_accountId0').value='';
        document.getElementById(str+'_CRamount0').value='';
        document.getElementById(str+'_DRamount0').value='';
        document.getElementById(str+'_description0').value='';        
}

function check_and_submit(id)
{
        var form = document.getElementById(id);
        form.action='/bin/finance.py/edit_sub_journal_complete';
        var g_description = document.getElementsByName("g_description");
        for(i=0;i<g_description.length;i++){
                if(g_description[i].value==""){
                        alert("please enter description!");
                        return;
                }                
        }
        if(check_balance(gSumOldDrValue,gSumOldCrValue,0,0,1,2)){
                form.submit();
        }else{
                alert('Not balance!');
                return;
        }
}
function disabled_obj(flag){
		var obj1 = document.getElementById(flag+'_DRamount0');
		var obj2 = document.getElementById(flag+'_CRamount0');
		if(obj1.value.trim().length==0 && obj2.value.trim().length==0){
				//obj1.disabled=null;
				//obj2.disabled=null;
				obj1.style.display='block';
				obj2.style.display='block';
				return;
		}
		if(obj1.value.trim().length>0){
				//obj2.disabled='disabled';
				obj2.style.display='none';
				return;
		}
		if(obj2.value.trim().length>0){
				obj1.style.display='none';  
				//obj1.disabled='disabled';
				return;
		}   
}
var i=0;
var j=0;
var dSumDrValue = 0;
var dSumCrValue = 0;
var gSumOldDrValue=0;
var gSumOldCrValue=0;
function init(){
{% for item in sub_journal_items %}
		var dl = document.createElement('dl');
		dl.setAttribute('class','fin-title fin-form2');
		dl.setAttribute('className','fin-title fin-form2');  
		dl.setAttribute('id','ditem'+i);
		
		var dd = document.createElement('dd');
		dd.innerHTML='<input type="hidden" name="old_d_accountId" value = "{{item['id']}}" /><a href="/bin/finance.py/show_account_details?a={{item['account_id']}}">{{item['account_id']}}</a>';
		dl.appendChild(dd);
		
		{% if item['drcr'] == 'DR' %}
				gSumOldDrValue += parseFloat({{item['amount']}});
				dSumDrValue += parseFloat({{item['amount']}});;
				var dd = document.createElement('dd');        
				dd.innerHTML="{{item['amount']}}";
				dl.appendChild(dd);
		
				var dd = document.createElement('dd');
				dd.innerHTML="&nbsp;";
				dl.appendChild(dd);
				
				var dd = document.createElement('dd');
				dd.innerHTML="{{item['description']}}";
				dl.appendChild(dd);
				
				var dt = document.createElement('dt');
				dt.innerHTML='<a href="javascript:void(0)" onclick="del_row(\'ditem'+i+'\');check_balance(gSumOldDrValue,gSumOldCrValue,{{item['amount']}},0,0,1);">Delete</a>';
				dl.appendChild(dt);
		{% else %}
				gSumOldCrValue += parseFloat({{item['amount']}});
				dSumCrValue += parseFloat({{item['amount']}});
				
				var dd = document.createElement('dd');        
				dd.innerHTML="&nbsp;";
				dl.appendChild(dd);
		
				var dd = document.createElement('dd');
				dd.innerHTML="{{item['amount']}}";
				dl.appendChild(dd);
				
				var dd = document.createElement('label');
				dd.innerHTML="{{item['description']}}";
				dl.appendChild(dd);
				
				var dt = document.createElement('dt');
				dt.innerHTML='<a href="javascript:void(0)" onclick="del_row(\'ditem'+i+'\');check_balance(gSumOldDrValue,gSumOldCrValue,0,{{item['amount']}},0,1);">Delete</a>';
				dl.appendChild(dt);
		{% endif %}        
		obj1.appendChild(dl);        
		i += 1;  
{% endfor %}


{% for item in journal_items %}
		{% if item['account_id'] == g_account_id %}
				var dl = document.createElement('dl');
				dl.setAttribute('class','fin-title fin-form2');
				dl.setAttribute('className','fin-title fin-form2');
				dl.setAttribute('id','gitem'+j); 
				
				var dd = document.createElement('dd');  
				dd.innerHTML='<input type="hidden" name="old_g_accountId" value = "{{item['id']}}" /><a href="/bin/finance.py/show_account_details?a={{item['account_id']}}">{{item['account_id']}}</a>';
				dl.appendChild(dd);
				
				{% if item['drcr'] == 'DR' %}
						var dd = document.createElement('dd');
						dd.setAttribute('id','td_g_DR'); 
						dd.innerHTML='<input type="hidden" name="old_g_DRamount" value="{{item['amount']}}" />{{item['amount']}}';
						dl.appendChild(dd);
				
						var dd = document.createElement('dd');
						dd.setAttribute('id','td_g_CR'); 
						dd.innerHTML='<input type="hidden" name="old_g_CRamount" value="" />&nbsp;';
						dl.appendChild(dd);
						
						var dd = document.createElement('dd');
						dd.innerHTML = '<input type="text" name="old_g_description" value="{{item['description']}}" />';
						dl.appendChild(dd);
						
						var dt = document.createElement('dt');
						dt.innerHTML='&nbsp;';
						dl.appendChild(dt);
				{% else %}
						var dd = document.createElement('dd');
						dd.setAttribute('id','td_g_DR'); 
						dd.innerHTML='<input type="hidden" name="old_g_DRamount" value="" />&nbsp;';
						dl.appendChild(dd);
				
						var dd = document.createElement('dd');
						dd.setAttribute('id','td_g_CR'); 
						dd.innerHTML='<input type="hidden" name="old_g_CRamount" value="{{item['amount']}}" />{{item['amount']}}';
						dl.appendChild(dd);
						
						var dd = document.createElement('dd');
						dd.innerHTML = '<input type="text" name="old_g_description" value="{{item['description']}}" />';
						var input = document.createElement('input');
						var description = document.getElementById('g_description').value;             
						dl.appendChild(dd); 
						
						var dt = document.createElement('dt');
						dt.innerHTML='&nbsp';
						dl.appendChild(dt);
				{% endif %}
				obj2.appendChild(dl);
				j += 1;
		{% endif %}
{% endfor %}

{% for item in journal_items %}
		{% if item['account_id'] != g_account_id %}
				var dl = document.createElement('dl');
				dl.setAttribute('class','fin-title fin-form2');
				dl.setAttribute('className','fin-title fin-form2');
				dl.setAttribute('id','gitem'+j); 
				
				var dd = document.createElement('dd');
				dd.setAttribute('class','wid');        
				dd.innerHTML='<input type="hidden" name="old_g_accountId" value = "{{item['id']}}" /><a href="/bin/finance.py/show_account_details?a={{item['account_id']}}">{{item['account_id']}}</a>';
				dl.appendChild(dd);
				
				{% if item['drcr'] == 'DR' %}
						gSumOldDrValue += parseFloat({{item['amount']}});
						var dd = document.createElement('dd');
						dd.innerHTML="{{item['amount']}}";
						dl.appendChild(dd);
		
						var dd = document.createElement('dd');        
						dd.innerHTML="&nbsp;";
						dl.appendChild(dd); 
						
						var dd = document.createElement('dd');        
						dd.innerHTML="{{item['description']}}";
						dl.appendChild(dd); 
						
						var dt = document.createElement('dt');        
						dt.innerHTML='<a href="javascript:void(0)" onclick="del_row(\'gitem'+j+'\');check_balance(gSumOldDrValue,gSumOldCrValue,{{item['amount']}},0,0,0);">Delete</a>';
						dl.appendChild(dt); 
				{% else %}
						gSumOldCrValue += parseFloat({{item['amount']}});
						var dd = document.createElement('dd');
						dd.innerHTML="&nbsp;";
						dl.appendChild(dd);
		
						var dd = document.createElement('dd');        
						dd.innerHTML="{{item['amount']}}";
						dl.appendChild(dd); 
						
						var dd = document.createElement('dd');        
						dd.innerHTML="{{item['description']}}";
						dl.appendChild(dd); 
						
						var dt = document.createElement('dt');        
						dt.innerHTML='<a href="javascript:void(0)" onclick="del_row(\'gitem'+j+'\');check_balance(gSumOldDrValue,gSumOldCrValue,0,{{item['amount']}},0,0);">Delete</a>';
						dl.appendChild(dt); 
				{% endif %}
				obj2.appendChild(dl);
				j += 1;
		{% endif %}
{% endfor %} 					   
check_balance(gSumOldDrValue,gSumOldCrValue,0,0,0,0);
}
